package com.tuhu.server.web.controller.serviceimpl;

import com.tuhu.server.db.bean.ActivateRecordBean;
import com.tuhu.server.db.op.ActivateRecordDB;
import com.tuhu.server.util.StringUtils;
import com.tuhu.server.util.TimeUtil;
import com.tuhu.server.web.bean.ActivateResultBean;
import com.tuhu.server.web.bean.InsertActiveCodeBean;
import com.tuhu.server.web.bean.MockResultBean;
import com.tuhu.server.web.controller.service.AccountService;
import com.tuhu.server.web.resp.ResCode;
import com.tuhu.server.web.resp.ResultBean;
import com.tuhu.server.web.resp.helper.ResponseCreator;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
public class AccountServiceImpl implements AccountService {

    @Override
    public ResponseEntity<ResultBean<ActivateResultBean>> activate(
            String activeCode, String deviceId, String brand, String model) {

        long KL = 1000L;

        ActivateResultBean resultBean = new ActivateResultBean();

        ActivateRecordBean dbBean = ActivateRecordDB.find(activeCode);
        if (dbBean == null) {
            resultBean.flag = false;
            resultBean.active_code = activeCode;
            return ResponseCreator.creatOk(resultBean, ResCode.CODE_ERR_UNKNOWN, "激活失败，激活码不存在");
        } else {

            if (StringUtils.isEmpty(dbBean.activate_time)) {
                //首次激活

                String timeNow = TimeUtil.convertYYYYMMDDHHMMSS(System.currentTimeMillis());

                //激活码类型，1:小时，2:天，3:周，4:月，5:季，6:半年，7:年，8:永久
                long[] vipDurationArr = {60 * 60 * 1000, 24 * 60 * 60 * KL, 7 * 24 * 60 * 60 * KL,
                        30 * 24 * 60 * 60 * KL, 90 * 24 * 60 * 60 * KL, 180 * 24 * 60 * 60 * KL,
                        365 * 24 * 60 * 60 * KL};

                String vip_end_time = "";
                if (dbBean.active_code_type >= 1 && dbBean.active_code_type <= 7) {
                    int index = dbBean.active_code_type - 1;
                    vip_end_time = TimeUtil.convertYYYYMMDDHHMMSS(System.currentTimeMillis() + vipDurationArr[index]);
                } else if (dbBean.active_code_type == 8) {
                    vip_end_time = "2099-12-31 23:59:59";
                } else {
                    return ResponseCreator.creatOk(resultBean, ResCode.CODE_ERR_UNKNOWN, "激活失败，激活码数据类型异常");
                }

                ActivateRecordDB.activate(activeCode, deviceId, brand, model, timeNow, vip_end_time);

                resultBean.flag = true;
                resultBean.active_code = dbBean.active_code;
                resultBean.end_time = vip_end_time;

                return ResponseCreator.creatOk(resultBean, ResCode.CODE_OK, "激活成功");
            } else {
                //二次激活，判断是否变更设备

                //检验是否更换设备
                if (StringUtils.equals(deviceId, dbBean.device_id)) {
                    //设备相同，则允许激活（例如用户在相同手机上卸载重装APP）

                    if (StringUtils.compare(TimeUtil.convertYYYYMMDDHHMMSS(System.currentTimeMillis()), dbBean.vip_end_time) < 0) {
                        //有效期内
                        resultBean.flag = true;
                        resultBean.active_code = dbBean.active_code;
                        resultBean.end_time = dbBean.vip_end_time;
                        return ResponseCreator.creatOk(resultBean, ResCode.CODE_OK, "激活成功");
                    } else {
                        //已过期
                        resultBean.flag = false;
                        resultBean.active_code = dbBean.active_code;
                        resultBean.end_time = dbBean.vip_end_time;
                        return ResponseCreator.creatOk(resultBean, ResCode.CODE_ERR_UNKNOWN, "激活码已过期");
                    }
                } else {
                    //设备不同，已更换设备，则不允许多次激活，否则造成一码多用

                    resultBean.flag = false;
                    resultBean.active_code = dbBean.active_code;
                    resultBean.end_time = dbBean.vip_end_time;
                    return ResponseCreator.creatOk(resultBean, ResCode.CODE_ERR_UNKNOWN, "此激活码已绑定其它设备，请先解绑");
                }

            }

        }

    }

    @Override
    public ResponseEntity<ResultBean<MockResultBean>> mock(String activeCode, String deviceId, String brand, String model, double lat, double lng) {

        ActivateRecordBean dbBean = ActivateRecordDB.find(activeCode);

        if (dbBean == null) {
            return ResponseCreator.creatOk(null, ResCode.CODE_ERR_UNKNOWN, "启动失败，激活码无效");
        }

        if (!StringUtils.equals(deviceId, dbBean.device_id)) {
            return ResponseCreator.creatOk(null, ResCode.CODE_ERR_UNKNOWN, "启动失败，设备已变更");
        }

        if (StringUtils.compare(TimeUtil.convertYYYYMMDDHHMMSS(System.currentTimeMillis()), dbBean.vip_end_time) > 0) {
            return ResponseCreator.creatOk(null, ResCode.CODE_ERR_UNKNOWN, "启动失败，激活码已过期");
        }

        MockResultBean mockResultBean = new MockResultBean();
        mockResultBean.lat = lat;
        mockResultBean.lng = lng;
        mockResultBean.flag = true;

        String timeNow = TimeUtil.convertYYYYMMDDHHMMSS(System.currentTimeMillis());

        ActivateRecordDB.mockUpdate(dbBean.active_code, timeNow);

        return ResponseCreator.creatOk(mockResultBean, ResCode.CODE_OK, "启动成功");
    }

    @Override
    public ResponseEntity<ResultBean<InsertActiveCodeBean>> insert_active_code(String activeCode, int activeCodeType,
                                                                               String password) {

        if (!StringUtils.equals(password, "781OHFQR01FFAEDF")) {
            return ResponseCreator.creatOk(null, ResCode.CODE_ERR_UNKNOWN, "");
        }

        String timeNow = TimeUtil.convertYYYYMMDDHHMMSS(System.currentTimeMillis());

        ActivateRecordDB.insert(activeCode, activeCodeType, timeNow);

        InsertActiveCodeBean activeCodeBean = new InsertActiveCodeBean();

        activeCodeBean.active_code = activeCode;
        activeCodeBean.active_code_type = activeCodeType;


        return ResponseCreator.creatOk(activeCodeBean, ResCode.CODE_OK, "添加成功");
    }

    @Override
    public ResponseEntity<ResultBean<Void>> unbind(String activeCode, String password) {

        if (!StringUtils.equals(password, "781OHFQR01FFAEDF")) {
            return ResponseCreator.creatOk(null, ResCode.CODE_ERR_UNKNOWN, "");
        }

        ActivateRecordBean dbBean = ActivateRecordDB.find(activeCode);
        if (dbBean == null) {
            return ResponseCreator.creatOk(null, ResCode.CODE_ERR_UNKNOWN, "解绑失败，激活码不存在");
        }

        if (StringUtils.isEmpty(dbBean.activate_time)) {
            return ResponseCreator.creatOk(null, ResCode.CODE_ERR_UNKNOWN, "解绑失败，激活码尚未使用，无需解绑");
        }

        boolean result =  ActivateRecordDB.unbind(activeCode);

        if (result) {
            return ResponseCreator.creatOk(null, ResCode.CODE_OK, "解绑成功");
        }

        return ResponseCreator.creatOk(null, ResCode.CODE_ERR_UNKNOWN, "解绑失败");
    }

    @Override
    public ResponseEntity<ResultBean<ActivateRecordBean>> query(String activeCode, String password) {

        if (!StringUtils.equals(password, "781OHFQR01FFAEDF")) {
            return ResponseCreator.creatOk(null, ResCode.CODE_ERR_UNKNOWN, "");
        }

        ActivateRecordBean dbBean = ActivateRecordDB.find(activeCode);
        if (dbBean == null) {
            return ResponseCreator.creatOk(null, ResCode.CODE_ERR_UNKNOWN, "查询失败，激活码不存在");
        }

        return ResponseCreator.creatOk(dbBean, ResCode.CODE_OK, "查询成功");
    }



}
